package com.vancouver.knows.business.tracker;

import java.util.HashMap;
import java.util.List;

import com.pangu.bean.PageableResult;
import com.pangu.task.IResultReceiver;
import com.pangu.task.OperateResult;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.MyArticleInfo;
import com.vancouver.knows.business.bean.MyAtcommentsInfo;
import com.vancouver.knows.business.bean.MyCommentsInfo;
import com.vancouver.knows.business.bean.MyFollowedInfo;
import com.vancouver.knows.business.service.impl.MyArticleHandle;
import com.vancouver.knows.business.task.mark.FetchMyUserPostCommentsTaskMark;
import com.vancouver.knows.business.task.mark.FollowedTaskMark;
import com.vancouver.knows.business.task.mark.MyArticleTaskMark;
import com.vancouver.knows.business.task.mark.MyAtcommentTaskMark;
import com.vancouver.knows.business.task.mark.MyCommentTaskMark;

/**
 * 我的文章列表
 * 
 * @author llf
 * 
 */
public class MyArticleFollowCommentTracker extends ABInvokeTracker {

	public MyArticleFollowCommentTracker(BusinessModule module, IResultReceiver resultReceiver) {
		super(module, resultReceiver);
	}

	@Override
	protected void handleTaskRefresh(ATaskMark taskMark, OperateResult result) {
		if (taskMark instanceof MyArticleTaskMark) {
			module.getCacheManager().getMyArticleCache().cleanCache();
		} else if (taskMark instanceof FollowedTaskMark) {
			module.getCacheManager().getMyFollowedCache().deleteItemInfoIndexFromCache(taskMark);
		} else if (taskMark instanceof MyCommentTaskMark) {
			module.getCacheManager().getMyCommentCache().cleanCache();
		} else if (taskMark instanceof MyAtcommentTaskMark) {
			module.getCacheManager().getMyAtcommentCache().cleanCache();
		} else if (taskMark instanceof FetchMyUserPostCommentsTaskMark) {
			module.getCacheManager().getMyUserPostCommentCache().cleanCache();
		}
	}

	@Override
	public String TAG() {
		return MyArticleFollowCommentTracker.class.getSimpleName();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void handleResult(OperateResult result) {
		PageableResult pageableResult = (PageableResult) result.getResultData();
		if (pageableResult.getContent() != null) {
			HashMap<String, Object> map = (HashMap<String, Object>) pageableResult.getContent();
			ATaskMark taskMark = result.getTaskMark();

			if (taskMark instanceof MyArticleTaskMark) {
				List<MyArticleInfo> myArticleList = (List<MyArticleInfo>) map.get(MyArticleHandle.ARTICLE_MAP);
				MyArticleTaskMark myArticleTaskMark = (MyArticleTaskMark) taskMark;
				myArticleTaskMark.setPageInfo(pageableResult.getPageInfo());
				module.getCacheManager().getMyArticleCache().addItemInfoToCache(myArticleTaskMark, myArticleList);
			} else if (taskMark instanceof FollowedTaskMark) {
				List<MyFollowedInfo> myFollowedList = (List<MyFollowedInfo>) map.get(MyArticleHandle.ARTICLE_MAP);
				FollowedTaskMark myFollowedTaskMark = (FollowedTaskMark) taskMark;
				myFollowedTaskMark.setPageInfo(pageableResult.getPageInfo());
				module.getCacheManager().getMyFollowedCache().addItemInfoToCache(myFollowedTaskMark, myFollowedList);
			} else if (taskMark instanceof MyCommentTaskMark) {
				List<MyCommentsInfo> myCommentList = (List<MyCommentsInfo>) map.get(MyArticleHandle.ARTICLE_MAP);
				MyCommentTaskMark myCommentTaskMark = (MyCommentTaskMark) taskMark;
				myCommentTaskMark.setPageInfo(pageableResult.getPageInfo());
				module.getCacheManager().getMyCommentCache().addItemInfoToCache(myCommentTaskMark, myCommentList);
			} else if (taskMark instanceof MyAtcommentTaskMark) {
				List<MyAtcommentsInfo> myCommentList = (List<MyAtcommentsInfo>) map.get(MyArticleHandle.ARTICLE_MAP);
				MyAtcommentTaskMark myAtCommentTaskMark = (MyAtcommentTaskMark) taskMark;
				myAtCommentTaskMark.setPageInfo(pageableResult.getPageInfo());
				module.getCacheManager().getMyAtcommentCache().addItemInfoToCache(myAtCommentTaskMark, myCommentList);
			} else if (taskMark instanceof FetchMyUserPostCommentsTaskMark) {
				List<MyCommentsInfo> myCommentList = (List<MyCommentsInfo>) map.get(MyArticleHandle.ARTICLE_MAP);
				FetchMyUserPostCommentsTaskMark myUserPostCommentsTaskMark = (FetchMyUserPostCommentsTaskMark) taskMark;
				myUserPostCommentsTaskMark.setPageInfo(pageableResult.getPageInfo());
				module.getCacheManager().getMyUserPostCommentCache().addItemInfoToCache(myUserPostCommentsTaskMark, myCommentList);
			}
		}
	}
}
